<#include "/includes/vars.inc"> 
<#assign packageDir = project.@rootPackage?replace(".","/")>
<@pp.dropOutputFile />
<#assign fileName = "${javaDir}${packageDir}/web/support/LoginController.java">
<#if includeLoginSpring && !pp.outputFileExists(fileName)>
	<@pp.changeOutputFile name="${fileName}" />

<#list entities.entity as entity>
    <#if entity.@name = config.login.@entity>
	<#assign entityName = entity.@name>
		
package ${project.@rootPackage}.web.support;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import ${project.@rootPackage}.service.support.AuthenticationService;
import ${project.@rootPackage}.web.util.JsfUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * LoginController
 * <#if project.@author[0]??>@author ${project.@author}</#if>
 */
@Controller(value = "loginController")
@Scope("session")
public class LoginController {
	private static final long serialVersionUID = 1L;
	protected static final Logger logger = Logger.getLogger("LoginController");
    private String login;
    private String password;
    @Autowired
    private AuthenticationService authenticationService;


    public String login() {
        //System.out.println("login: " + login + " " + password);
        boolean success = authenticationService.login(login, password);

        if (success) {
            JsfUtil.addSuccessMessage(JsfUtil.getMessage("loginInfo") + " " + getLogin());
            logger.log(Level.INFO, "Login: {0} {1} ", new Object[]{getLogin(),new Date()});
        } else {
            logger.log(Level.SEVERE, "Login: {0} {1} ", new Object[]{login,password});
            JsfUtil.addErrorMessage("formLogin:login", JsfUtil.getMessage("loginError"));
        }
        return null;
    }

    public void logout() {
        logger.log(Level.INFO, "Logout: {0} {1}", new Object[]{getLogin(),new Date()});
        authenticationService.logout();
        //JsfUtil.closeSession();
        JsfUtil.redirect("/login.jsf?faces-redirect=true");
    }

    public boolean isLoggedIn() {
        return authenticationService.isLoggedIn();
    }

    public String getUserName() {
        //System.out.println(authenticationService.getUserDetails());
        return authenticationService.getUsername();
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAuthenticationService(AuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }
}

	</#if>
</#list>

</#if>
